/*****************************************************************************
*        描述 :
*             文件描述
*        CLR版本:            3.5
*        创建者 :            蒋 光
*        个人域名:           jiangguang.net.cn
*        命名空间名称:       ECP.DataAccess
*        文件名:             BaseMessageDao.cs
*        创建系统时间:       2012/11/2 19:06:59
*        创建年份:           2012
*
/*****************************************************************************/

namespace ECP.DataAccess
{
    using System;
    using System.Data;
    using ECP.DataAccess.Utilities;
    using ECP.Model;
    using ECP.Utilities;

    public class BaseMessageDao : BaseDao
    {
        private string Query;

        public BaseMessageDao()
        {
            this.Query = " SELECT * FROM " + BaseMessageTable.TableName;
            base.CurrentTableName = BaseMessageTable.TableName;
        }

        public BaseMessageDao(IDbHelper dbHelper)
            : this()
        {
            base.DbHelper = dbHelper;
        }

        public BaseMessageDao(BaseUserInfo userInfo)
            : this()
        {
            base.UserInfo = userInfo;
        }

        public BaseMessageDao(IDbHelper dbHelper, BaseUserInfo userInfo)
            : this(dbHelper)
        {
            base.UserInfo = userInfo;
        }

        public string Add(BaseMessageEntity messageEntity)
        {
            return this.AddEntity(messageEntity);
        }

        public string AddEntity(BaseMessageEntity messageEntity)
        {
            string sequence = BaseSequenceDao.Instance.GetSequence(base.DbHelper, BaseMessageTable.TableName);
            messageEntity.SortCode = sequence;
            SQLBuilder sqlBuilder = new SQLBuilder(base.DbHelper);
            sqlBuilder.BeginInsert(BaseMessageTable.TableName);
            sqlBuilder.SetValue(BaseMessageTable.FieldID, messageEntity.ID);
            this.SetEntity(sqlBuilder, messageEntity);
            sqlBuilder.SetValue(BaseMessageTable.FieldCreateUserID, base.UserInfo.ID);
            sqlBuilder.SetValue(BaseMessageTable.FieldCreateUserRealname, base.UserInfo.Realname);
            sqlBuilder.SetDBNow(BaseMessageTable.FieldCreateDate);
            if (sqlBuilder.EndInsert() <= 0)
            {
                return string.Empty;
            }
            return messageEntity.ID;
        }

        public int BatchSend(string[] receiverIDs, string[] organizeIDs, string[] roleIDs, BaseMessageEntity messageEntity, bool saveSend)
        {
            string[] iDs = null;
            string[] strArray2 = null;
            if (organizeIDs != null)
            {
                BaseStaffDao dao = new BaseStaffDao(base.DbHelper);
                iDs = dao.GetIDs(BaseStaffTable.FieldCompanyID, (object[])organizeIDs);
                strArray2 = dao.GetIDs(BaseStaffTable.FieldDepartmentID, (object[])organizeIDs);
            }
            string[] strArray3 = null;
            if (roleIDs != null)
            {
                strArray3 = new BaseUserRoleDao(base.DbHelper).GetIDs(BaseUserRoleTable.FieldRoleID, (object[])roleIDs, BaseUserRoleTable.FieldUserID);
            }
            receiverIDs = BaseBusinessLogic.Concat(new string[][] { receiverIDs, iDs, strArray2, strArray3 });
            return this.Send(messageEntity, receiverIDs, saveSend);
        }

        public DataTable GetDeletedDT()
        {
            DataTable dataTable = new DataTable(BaseMessageTable.TableName);
            string sqlQuery = this.Query + " WHERE (" + BaseMessageTable.FieldDeleteMark + " = 1 )  AND (" + BaseMessageTable.FieldReceiverID + " = " + base.DbHelper.GetParameter(BaseMessageTable.FieldReceiverID) + " )  ORDER BY " + BaseMessageTable.FieldSortCode;
            base.DbHelper.Fill(dataTable, sqlQuery, base.DbHelper.MakeParameters(BaseMessageTable.FieldReceiverID, base.UserInfo.ID));
            return dataTable;
        }

        public DataTable GetDTByFunction(string categoryID, string functionID, string userID)
        {
            string sqlQuery = this.Query + "    WHERE (" + BaseMessageTable.FieldDeleteMark + " = 0 )           AND (" + BaseMessageTable.FieldCategoryID + " = '" + categoryID + "') ";
            if (!string.IsNullOrEmpty(functionID))
            {
                string str2 = sqlQuery;
                sqlQuery = str2 + "          AND (" + BaseMessageTable.FieldFunctionID + " = '" + functionID + "' ) ";
            }
            if (categoryID.Equals(MessageCategory.Send.ToString()))
            {
                string str3 = sqlQuery;
                sqlQuery = str3 + "          AND (" + BaseMessageTable.FieldCreateUserID + " = " + base.DbHelper.GetParameter(BaseMessageTable.FieldReceiverID) + ") ";
            }
            else
            {
                string str4 = sqlQuery;
                sqlQuery = str4 + "          AND (" + BaseMessageTable.FieldReceiverID + " = " + base.DbHelper.GetParameter(BaseMessageTable.FieldReceiverID) + ") ";
            }
            string str5 = sqlQuery;
            sqlQuery = str5 + " ORDER BY " + BaseMessageTable.FieldIsNew + " DESC           ," + BaseMessageTable.FieldCreateDate;
            DataTable dataTable = new DataTable(BaseMessageTable.TableName);
            base.DbHelper.Fill(dataTable, sqlQuery, base.DbHelper.MakeParameters(BaseMessageTable.FieldReceiverID, userID));
            return dataTable;
        }

        public int GetNewCount()
        {
            return this.GetNewCount(MessageFunction.Message);
        }

        public int GetNewCount(MessageFunction messageFunction)
        {
            int num = 0;
            string[] strArray = new string[] { " SELECT COUNT(*)    FROM ", BaseMessageTable.TableName, "  WHERE (", BaseMessageTable.FieldIsNew, " = ", 1.ToString(), " )         AND (", BaseMessageTable.FieldReceiverID, " = '", base.UserInfo.ID, "' )        AND (", BaseMessageTable.FieldFunctionID, " = '", messageFunction.ToString(), "' )" };
            string sqlQuery = string.Concat(strArray);
            object obj2 = base.DbHelper.ExecuteScalar(sqlQuery);
            if (obj2 != null)
            {
                num = int.Parse(obj2.ToString());
            }
            return num;
        }

        public DataTable GetNewDT()
        {
            string[] strArray = new string[] { this.Query, "    WHERE (", BaseMessageTable.FieldIsNew, " = ", 1.ToString(), " )           AND (", BaseMessageTable.FieldReceiverID, " = ", base.DbHelper.GetParameter(BaseMessageTable.FieldReceiverID), ")  ORDER BY ", BaseMessageTable.FieldCreateUserID, "          ,", BaseMessageTable.FieldCreateDate };
            string sqlQuery = string.Concat(strArray);
            DataTable dataTable = new DataTable(BaseMessageTable.TableName);
            base.DbHelper.Fill(dataTable, sqlQuery, base.DbHelper.MakeParameters(BaseMessageTable.FieldReceiverID, base.UserInfo.ID));
            return dataTable;
        }

        public BaseMessageEntity GetNewOne()
        {
            BaseMessageEntity entity = new BaseMessageEntity();
            DataTable dataTable = new DataTable(BaseMessageTable.TableName);
            string[] strArray = new string[] { " SELECT *    FROM (SELECT * FROM ", BaseMessageTable.TableName, " WHERE (", BaseMessageTable.FieldIsNew, " = ", 1.ToString(), " )          AND (", BaseMessageTable.FieldReceiverID, " = '", base.UserInfo.ID, "')  ORDER BY ", BaseMessageTable.FieldCreateDate, " DESC)  WHERE ROWNUM = 1 " };
            string sqlQuery = string.Concat(strArray);
            base.DbHelper.Fill(dataTable, sqlQuery);
            return entity.GetFrom(dataTable);
        }

        public DataTable GetOldDT()
        {
            DataTable dataTable = new DataTable(BaseMessageTable.TableName);
            string[] strArray2 = new string[] { this.Query, " WHERE (", BaseMessageTable.FieldIsNew, " = ", 0.ToString(), " )  AND (", BaseMessageTable.FieldReceiverID, " = ", base.DbHelper.GetParameter(BaseMessageTable.FieldReceiverID), " )  ORDER BY ", BaseMessageTable.FieldSortCode };
            string sqlQuery = string.Concat(strArray2);
            string[] targetFileds = new string[1];
            object[] targetValues = new object[1];
            targetFileds[0] = BaseMessageTable.FieldReceiverID;
            targetValues[0] = base.UserInfo.ID;
            base.DbHelper.Fill(dataTable, sqlQuery, base.DbHelper.MakeParameters(targetFileds, targetValues));
            return dataTable;
        }

        public DataTable GetReceiveDT()
        {
            return this.GetDTByFunction(MessageCategory.Receiver.ToString(), MessageFunction.Message.ToString(), base.UserInfo.ID);
        }

        public DataTable GetSendDT()
        {
            DataTable dataTable = new DataTable(BaseMessageTable.TableName);
            string sqlQuery = this.Query + " WHERE (" + BaseMessageTable.FieldCategoryID + " = '" + MessageCategory.Send.ToString() + "')  AND (" + BaseMessageTable.FieldDeleteMark + " = 0)  AND (" + BaseMessageTable.FieldCreateUserID + " = '" + base.UserInfo.ID + "')  ORDER BY " + BaseMessageTable.FieldSortCode;
            base.DbHelper.Fill(dataTable, sqlQuery);
            return dataTable;
        }

        public DataTable GetWarningDT()
        {
            return this.GetDTByFunction(MessageCategory.Receiver.ToString(), MessageFunction.Warning.ToString(), base.UserInfo.ID);
        }

        public DataTable GetWarningDT(string userID)
        {
            return this.GetDTByFunction(MessageCategory.Receiver.ToString(), MessageFunction.Warning.ToString(), userID);
        }

        public string[] MessageChek()
        {
            string[] strArray = new string[6];
            int newCount = this.GetNewCount();
            strArray[0] = newCount.ToString();
            if (newCount > 0)
            {
                BaseMessageEntity newOne = this.GetNewOne();
                if (newOne.CreateDate.Length > 0)
                {
                    strArray[1] = DateTime.Parse(newOne.CreateDate).ToString(BaseSystemInfo.DateTimeFormat);
                }
                strArray[2] = newOne.CreateUserID;
                strArray[3] = newOne.CreateUserRealname;
                strArray[4] = newOne.ID;
                strArray[5] = newOne.Content;
            }
            return strArray;
        }

        private int OnRead(BaseMessageEntity messageEntity, string id)
        {
            int num = 0;
            if ((messageEntity.ReceiverID == base.UserInfo.ID) && (messageEntity.IsNew == 1))
            {
                int num2 = 0;
                num += this.SetProperty(id, BaseMessageTable.FieldIsNew, num2.ToString());
            }
            messageEntity.ReadCount++;
            this.SetProperty(id, BaseMessageTable.FieldReadCount, messageEntity.ReadCount.ToString());
            num++;
            return num;
        }

        public DataTable Read(string id)
        {
            DataTable dataTable = this.Get(id);
            BaseMessageEntity messageEntity = new BaseMessageEntity(dataTable);
            this.OnRead(messageEntity, id);
            return dataTable;
        }

        public DataTable ReadFromReceiver(string receiverID)
        {
            DataTable dataTable = new DataTable(BaseMessageTable.TableName);
            string[] strArray = new string[] { this.Query, " WHERE (", BaseMessageTable.FieldIsNew, " = ", 1.ToString(), " )  AND (", BaseMessageTable.FieldReceiverID, " = '", base.UserInfo.ID, "' )  AND (", BaseMessageTable.FieldCreateUserID, " = '", receiverID, "' )  ORDER BY ", BaseMessageTable.FieldSortCode };
            string sqlQuery = string.Concat(strArray);
            base.DbHelper.Fill(dataTable, sqlQuery);
            string id = string.Empty;
            foreach (DataRow row in dataTable.Rows)
            {
                if (row[BaseMessageTable.FieldReceiverID].ToString() == base.UserInfo.ID)
                {
                    id = row[BaseMessageTable.FieldID].ToString();
                    this.SetProperty(id, BaseMessageTable.FieldIsNew, 0);
                }
            }
            return dataTable;
        }

        public DataTable SearchDeletedDT(string search)
        {
            if (search.Length == 0)
            {
                return this.GetDeletedDT();
            }
            DataTable dataTable = new DataTable(BaseMessageTable.TableName);
            string sqlQuery = this.Query + " WHERE ((" + BaseMessageTable.FieldContent + " LIKE ? )  OR ( " + BaseMessageTable.FieldReceiverRealname + " LIKE ? )  OR (" + BaseMessageTable.FieldCreateDate + " LIKE ? ))  AND (" + BaseMessageTable.FieldDeleteMark + " = 1 )  AND (" + BaseMessageTable.FieldReceiverID + " = ? )  ORDER BY " + BaseMessageTable.FieldSortCode;
            string[] targetFileds = new string[4];
            object[] targetValues = new object[4];
            for (int i = 0; i < 3; i++)
            {
                targetFileds[i] = BaseMessageTable.FieldContent;
                targetValues[i] = search;
            }
            targetFileds[3] = BaseMessageTable.FieldReceiverID;
            targetValues[3] = base.UserInfo.ID;
            base.DbHelper.Fill(dataTable, sqlQuery, base.DbHelper.MakeParameters(targetFileds, targetValues));
            return dataTable;
        }

        public DataTable SearchNewList(string search)
        {
            if (search.Length == 0)
            {
                return this.GetNewDT();
            }
            DataTable dataTable = new DataTable(BaseMessageTable.TableName);
            string[] strArray2 = new string[] {
                this.Query, " WHERE ((", BaseMessageTable.FieldContent, " LIKE ", base.DbHelper.GetParameter(BaseMessageTable.FieldContent), " )  OR ( ", BaseMessageTable.FieldTitle, " LIKE ", base.DbHelper.GetParameter(BaseMessageTable.FieldReceiverID), " )  OR ( ", BaseMessageTable.FieldReceiverRealname, " LIKE ", base.DbHelper.GetParameter(BaseMessageTable.FieldReceiverID), " ))  AND (", BaseMessageTable.FieldIsNew, " = ",
                1.ToString(), " )  AND (", BaseMessageTable.FieldReceiverID, " = ", base.DbHelper.GetParameter(BaseMessageTable.FieldReceiverID), " )  ORDER BY ", BaseMessageTable.FieldSortCode
             };
            string sqlQuery = string.Concat(strArray2);
            string[] targetFileds = new string[4];
            object[] targetValues = new object[4];
            for (int i = 0; i < 3; i++)
            {
                targetFileds[i] = BaseMessageTable.FieldContent;
                targetValues[i] = search;
            }
            targetFileds[3] = BaseMessageTable.FieldReceiverID;
            targetValues[3] = base.UserInfo.ID;
            base.DbHelper.Fill(dataTable, sqlQuery, base.DbHelper.MakeParameters(targetFileds, targetValues));
            return dataTable;
        }

        public DataTable SearchOldDT(string search)
        {
            if (search.Length == 0)
            {
                return this.GetOldDT();
            }
            DataTable dataTable = new DataTable(BaseMessageTable.TableName);
            string[] strArray2 = new string[] {
                this.Query, " WHERE ((", BaseMessageTable.FieldContent, " LIKE ", base.DbHelper.GetParameter(BaseMessageTable.FieldContent), " )  OR (", BaseMessageTable.FieldReceiverRealname, " LIKE ", base.DbHelper.GetParameter(BaseMessageTable.FieldReceiverID), " )  OR (", BaseMessageTable.FieldCreateDate, " LIKE ", base.DbHelper.GetParameter(BaseMessageTable.FieldCreateDate), " ))  AND (", BaseMessageTable.FieldIsNew, " = ",
                0.ToString(), " )  AND (", BaseMessageTable.FieldReceiverID, " = ", base.DbHelper.GetParameter(BaseMessageTable.FieldReceiverID), " )  ORDER BY ", BaseMessageTable.FieldSortCode
             };
            string sqlQuery = string.Concat(strArray2);
            string[] targetFileds = new string[4];
            object[] targetValues = new object[4];
            for (int i = 0; i < 3; i++)
            {
                targetFileds[i] = BaseMessageTable.FieldContent;
                targetValues[i] = search;
            }
            targetFileds[3] = BaseMessageTable.FieldReceiverID;
            targetValues[3] = base.UserInfo.ID;
            base.DbHelper.Fill(dataTable, sqlQuery, base.DbHelper.MakeParameters(targetFileds, targetValues));
            return dataTable;
        }

        public DataTable SearchReceiveDT(string search)
        {
            if (search.Length == 0)
            {
                return this.GetReceiveDT();
            }
            search = BaseBusinessLogic.GetSearchString(search);
            string str2 = this.Query + "    WHERE (" + BaseMessageTable.FieldDeleteMark + " = 0 )           AND (" + BaseMessageTable.FieldCategoryID + " = '" + MessageCategory.Receiver.ToString() + "') ";
            string str3 = str2 + "          AND (" + BaseMessageTable.FieldFunctionID + " = '" + MessageFunction.Message.ToString() + "' ) ";
            string str4 = str3 + "          AND (" + BaseMessageTable.FieldReceiverID + " = " + base.DbHelper.GetParameter(BaseMessageTable.FieldReceiverID) + ") ";
            string str5 = str4 + " AND ((" + BaseMessageTable.FieldTitle + " LIKE " + base.DbHelper.GetParameter(BaseMessageTable.FieldTitle) + " )  OR (" + BaseMessageTable.FieldContent + " LIKE " + base.DbHelper.GetParameter(BaseMessageTable.FieldContent) + " )  OR (" + BaseMessageTable.FieldCreateUserRealname + " LIKE " + base.DbHelper.GetParameter(BaseMessageTable.FieldCreateUserRealname) + " )) ";
            string sqlQuery = str5 + " ORDER BY " + BaseMessageTable.FieldCreateDate + "          ," + BaseMessageTable.FieldCreateUserID;
            DataTable dataTable = new DataTable(BaseMessageTable.TableName);
            string[] targetFileds = new string[4];
            object[] targetValues = new object[4];
            targetFileds[0] = BaseMessageTable.FieldReceiverID;
            targetValues[0] = base.UserInfo.ID;
            targetFileds[1] = BaseMessageTable.FieldTitle;
            targetValues[1] = search;
            targetFileds[2] = BaseMessageTable.FieldContent;
            targetValues[2] = search;
            targetFileds[3] = BaseMessageTable.FieldCreateUserRealname;
            targetValues[3] = base.UserInfo.ID;
            base.DbHelper.Fill(dataTable, sqlQuery, base.DbHelper.MakeParameters(targetFileds, targetValues));
            return dataTable;
        }

        public DataTable SearchSendDT(string search)
        {
            if (search.Length == 0)
            {
                return this.GetSendDT();
            }
            search = BaseBusinessLogic.GetSearchString(search);
            DataTable dataTable = new DataTable(BaseMessageTable.TableName);
            string sqlQuery = this.Query + " WHERE ((" + BaseMessageTable.FieldContent + " LIKE " + base.DbHelper.GetParameter(BaseMessageTable.FieldContent) + " )  OR (" + BaseMessageTable.FieldReceiverRealname + " LIKE " + base.DbHelper.GetParameter(BaseMessageTable.FieldReceiverRealname) + " )  OR (" + BaseMessageTable.FieldCreateDate + " LIKE " + base.DbHelper.GetParameter(BaseMessageTable.FieldCreateDate) + " ))  AND (" + BaseMessageTable.FieldDeleteMark + " = 0)  AND (" + BaseMessageTable.FieldCategoryID + " = '" + MessageCategory.Send.ToString() + "')  AND (" + BaseMessageTable.FieldCreateUserID + " = " + base.DbHelper.GetParameter(BaseMessageTable.FieldCreateUserID) + " )  ORDER BY " + BaseMessageTable.FieldSortCode;
            string[] targetFileds = new string[4];
            object[] targetValues = new object[4];
            targetFileds[0] = BaseMessageTable.FieldContent;
            targetValues[0] = search;
            targetFileds[1] = BaseMessageTable.FieldReceiverRealname;
            targetValues[1] = search;
            targetFileds[2] = BaseMessageTable.FieldCreateDate;
            targetValues[2] = search;
            targetFileds[3] = BaseMessageTable.FieldCreateUserID;
            targetValues[3] = base.UserInfo.ID;
            base.DbHelper.Fill(dataTable, sqlQuery, base.DbHelper.MakeParameters(targetFileds, targetValues));
            return dataTable;
        }

        public DataTable SearchWarningDT(string search)
        {
            return this.SearchWarningDT(search, base.UserInfo.ID);
        }

        public DataTable SearchWarningDT(string search, string userID)
        {
            if (search.Length == 0)
            {
                return this.GetWarningDT();
            }
            search = BaseBusinessLogic.GetSearchString(search);
            string str2 = this.Query + "    WHERE (" + BaseMessageTable.FieldDeleteMark + " = 0 )           AND (" + BaseMessageTable.FieldCategoryID + " = '" + MessageCategory.Receiver.ToString() + "') ";
            string str3 = str2 + "          AND (" + BaseMessageTable.FieldFunctionID + " = '" + MessageFunction.Warning.ToString() + "' ) ";
            string str4 = str3 + "          AND (" + BaseMessageTable.FieldReceiverID + " = " + base.DbHelper.GetParameter(BaseMessageTable.FieldReceiverID) + ") ";
            string str5 = str4 + " AND ((" + BaseMessageTable.FieldTitle + " LIKE " + base.DbHelper.GetParameter(BaseMessageTable.FieldTitle) + " )  OR (" + BaseMessageTable.FieldContent + " LIKE " + base.DbHelper.GetParameter(BaseMessageTable.FieldContent) + " )  OR (" + BaseMessageTable.FieldCreateUserRealname + " LIKE " + base.DbHelper.GetParameter(BaseMessageTable.FieldCreateUserRealname) + " )) ";
            string sqlQuery = str5 + " ORDER BY " + BaseMessageTable.FieldIsNew + "          ," + BaseMessageTable.FieldCreateDate;
            DataTable dataTable = new DataTable(BaseMessageTable.TableName);
            string[] targetFileds = new string[4];
            object[] targetValues = new object[4];
            targetFileds[0] = BaseMessageTable.FieldReceiverID;
            targetValues[0] = base.UserInfo.ID;
            targetFileds[1] = BaseMessageTable.FieldTitle;
            targetValues[1] = search;
            targetFileds[2] = BaseMessageTable.FieldContent;
            targetValues[2] = search;
            targetFileds[3] = BaseMessageTable.FieldCreateUserRealname;
            targetValues[3] = userID;
            base.DbHelper.Fill(dataTable, sqlQuery, base.DbHelper.MakeParameters(targetFileds, targetValues));
            return dataTable;
        }

        public int Send(BaseMessageEntity messageEntity, bool saveSend)
        {
            string[] receiverIDs = new string[] { messageEntity.ReceiverID };
            return this.Send(messageEntity, receiverIDs, saveSend);
        }

        public int Send(BaseMessageEntity messageEntity, string organizeID, bool saveSend)
        {
            int num2 = 0;
            DataTable childrenStaffs = new BaseOrganizeDao(base.DbHelper, base.UserInfo).GetChildrenStaffs(organizeID);
            string[] receiverIDs = new string[childrenStaffs.Rows.Count];
            foreach (DataRow row in childrenStaffs.Rows)
            {
                receiverIDs[num2++] = row[BaseMessageTable.FieldID].ToString();
            }
            return this.Send(messageEntity, receiverIDs, saveSend);
        }

        public int Send(BaseMessageEntity messageEntity, string[] receiverIDs, bool saveSend)
        {
            BaseStaffDao dao = new BaseStaffDao(base.DbHelper, base.UserInfo);
            int num = 0;
            messageEntity.CategoryID = MessageCategory.Receiver.ToString();
            messageEntity.IsNew = 1;
            messageEntity.IPAddress = base.UserInfo.IPAddress;
            messageEntity.ParentID = null;
            num++;
            for (int i = 0; i < receiverIDs.Length; i++)
            {
                messageEntity.ParentID = null;
                messageEntity.ID = Guid.NewGuid().ToString();
                messageEntity.CategoryID = MessageCategory.Receiver.ToString();
                messageEntity.ReceiverID = receiverIDs[i];
                messageEntity.ReceiverRealname = dao.GetProperty(messageEntity.ReceiverID, BaseStaffTable.FieldRealname);
                if (messageEntity.ReceiverID.Equals(base.UserInfo.ID))
                {
                    messageEntity.IsNew = 0;
                }
                string str = this.AddEntity(messageEntity);
                if (saveSend)
                {
                    messageEntity.ID = Guid.NewGuid().ToString();
                    messageEntity.ParentID = str;
                    messageEntity.CategoryID = MessageCategory.Send.ToString();
                    this.AddEntity(messageEntity);
                }
                num++;
            }
            return num;
        }

        private void SetEntity(SQLBuilder sqlBuilder, BaseMessageEntity messageEntity)
        {
            sqlBuilder.SetValue(BaseMessageTable.FieldParentID, messageEntity.ParentID);
            sqlBuilder.SetValue(BaseMessageTable.FieldCategoryID, messageEntity.CategoryID);
            sqlBuilder.SetValue(BaseMessageTable.FieldFunctionID, messageEntity.FunctionID);
            sqlBuilder.SetValue(BaseMessageTable.FieldObjectID, messageEntity.ObjectID);
            sqlBuilder.SetValue(BaseMessageTable.FieldReceiverID, messageEntity.ReceiverID);
            sqlBuilder.SetValue(BaseMessageTable.FieldReceiverRealname, messageEntity.ReceiverRealname);
            sqlBuilder.SetValue(BaseMessageTable.FieldTitle, messageEntity.Title);
            sqlBuilder.SetValue(BaseMessageTable.FieldIsNew, messageEntity.IsNew);
            sqlBuilder.SetValue(BaseMessageTable.FieldContent, messageEntity.Content);
            sqlBuilder.SetValue(BaseMessageTable.FieldIPAddress, messageEntity.IPAddress);
            sqlBuilder.SetValue(BaseMessageTable.FieldReadCount, messageEntity.ReadCount);
            sqlBuilder.SetValue(BaseMessageTable.FieldTargetURL, messageEntity.TargetURL);
            sqlBuilder.SetValue(BaseMessageTable.FieldSortCode, messageEntity.SortCode);
            sqlBuilder.SetValue(BaseMessageTable.FieldDescription, messageEntity.Description);
            sqlBuilder.SetValue(BaseMessageTable.FieldEnabled, messageEntity.Enabled ? 1 : 0);
        }

        public int UpdateEntity(BaseMessageEntity messageEntity)
        {
            SQLBuilder sqlBuilder = new SQLBuilder(base.DbHelper);
            sqlBuilder.BeginUpdate(BaseMessageTable.TableName);
            this.SetEntity(sqlBuilder, messageEntity);
            sqlBuilder.SetValue(BaseMessageTable.FieldModifyUserID, base.UserInfo.ID);
            sqlBuilder.SetDBNow(BaseMessageTable.FieldModifyDate);
            sqlBuilder.SetWhere(BaseMessageTable.FieldID, messageEntity.ID);
            return sqlBuilder.EndUpdate();
        }
    }
}